package com.hlhj.funlike_business.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.FoodTypeAdp;
import com.hlhj.funlike_business.bean.AddGoodBean;
import com.hlhj.funlike_business.bean.FoodTypeBean;
import com.hlhj.funlike_business.presenter.MyFoodTypePresenter;
import com.hlhj.funlike_business.views.MyFoodTypeView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MyFoodTypeAty extends BaseActivity<MyFoodTypeView, MyFoodTypePresenter> implements MyFoodTypeView {
    private ImageView img_back, img_add;
    private TextView tv_title;
    private EditText ed_foodtype;
    private RecyclerView recycle_list;
    private FoodTypeAdp foodTypeAdp;
    private List<FoodTypeBean.DataBean> strings;

    @Override
    protected int getContentId() {
        return R.layout.aty_myfoodtype;
    }

    @Override
    protected void initData() {
        tv_title.setText("商品类型");
        strings = new ArrayList<>();
        foodTypeAdp = new FoodTypeAdp(strings);
        recycle_list.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycle_list.setAdapter(foodTypeAdp);
    }

    @NotNull
    @Override
    public MyFoodTypeView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MyFoodTypePresenter createPresenter() {
        return new MyFoodTypePresenter();
    }

    @Override
    public void initView() {
        tv_title = findViewById(R.id.tv_title);
        img_back = findViewById(R.id.img_back);
        img_add = findViewById(R.id.img_add);
        recycle_list = findViewById(R.id.recycle_list);
        ed_foodtype = findViewById(R.id.ed_foodtype);
    }

    @Override
    public void logicStart() {
        getPresenter().LoadGoodList(this);

        foodTypeAdp.setListener(new FoodTypeAdp.getResult() {
            @Override
            public void showDel(int id) {
                getPresenter().LoadDel(MyFoodTypeAty.this,id);
            }
        });
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == ed_foodtype || "".endsWith(ed_foodtype.getText().toString()) || TextUtils.isEmpty(ed_foodtype.getText().toString())) {
                    Toast.makeText(MyFoodTypeAty.this, "请输入添加的类型", Toast.LENGTH_SHORT).show();
                    return;
                }
               getPresenter().LoadAdd(MyFoodTypeAty.this,ed_foodtype.getText().toString());
            }
        });
    }

    @Override
    public void showDelResult(String msg) {
        Toast.makeText(MyFoodTypeAty.this,msg.toString(),Toast.LENGTH_SHORT).show();
        getPresenter().LoadGoodList(this);
    }

    @Override
    public void showAddResult(AddGoodBean addGoodBean) {
        Toast.makeText(MyFoodTypeAty.this,addGoodBean.getMsg().toString(),Toast.LENGTH_SHORT).show();
        getPresenter().LoadGoodList(this);
    }

    @Override
    public void showGoodList(List<FoodTypeBean.DataBean> dataBeans) {
        strings.clear();
        strings.addAll(dataBeans);
        foodTypeAdp.notifyDataSetChanged();
        ed_foodtype.setText("");
    }
}
